//
//  ViewController.m
//  19-JSON
//
//  Created by 曹曹 on 16/8/9.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"
#import "NetEaseTitleLayout.h"
#import "TitleCollectionViewCell.h"
#import "ContentTitle.h"

@interface ViewController () <UICollectionViewDelegate, UICollectionViewDataSource, NetEaseTitltLayoutDelegate>
@property (nonatomic, strong) NSMutableArray<ContentTitle *> *titleArray;
@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;
@property (nonatomic, strong) NetEaseTitleLayout *layout;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
#pragma mark - JSON解析
    // 通过一个data, 将JSON格式转换成OC认识的内容:
    // 数组/字典
    // 获取JSON文件的路径
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"FirstJSON" ofType:@"json"];
    // 转化成NSData
    NSData *jsonData = [NSData dataWithContentsOfFile:dataPath];
    // JSON解析
    NSError *error = nil;
    NSDictionary *jsonDiction = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];/**< (**)发生错误时会给出反馈 */
#pragma mark - 可以取到对应的layout对象
    NSLog(@"%@", self.myCollectionView.collectionViewLayout);
    self.layout = (NetEaseTitleLayout *)self.myCollectionView.collectionViewLayout;
    _layout.titleFont = [UIFont systemFontOfSize:30];
    _layout.delegate = self;
    NSArray *list = jsonDiction[@"tabs"][@"list"];
    self.titleArray = [NSMutableArray array];
    for (NSDictionary *dictionary in list) {
        ContentTitle *contentTitle = [ContentTitle contentTitleWithDictionary:dictionary];
        [_titleArray addObject:contentTitle];
    }
}
#pragma mark - collectionView delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _titleArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionReuse" forIndexPath:indexPath];
    cell.titleLabel.font = _layout.titleFont;
    cell.content = _titleArray[indexPath.row];
    cell.contentView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:0.5];
    return cell;
}
#pragma mark - layout delegate
- (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout *)collectionViewLayout
  widthForItemAtIndexPath:(NSIndexPath *)indexPath {
    ContentTitle *contentTitle = _titleArray[indexPath.row];
    NSString *str = contentTitle.title;
    CGRect newRect = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGRectGetHeight(self.myCollectionView.bounds))
                                       options:NSStringDrawingUsesLineFragmentOrigin
                                    attributes:@{NSFontAttributeName : _layout.titleFont}
                                       context:nil];
    CGFloat width = ceil(newRect.size.width);/**< 向上取整 */
    return width;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
